const fs = require('fs');
const tool = {
    getFilesAndFoldersInDir: function (path) {
        const items = fs.readdirSync(path);
        const result = [];
        items.forEach(item => {
            const itemPath = `${path}/${item}`;
            const stat = fs.statSync(itemPath);
            if (stat.isDirectory()) {
                let data = {
                    // 文件夹
                    type: 'folder',
                    name: item
                }
                let children = this.getFilesAndFoldersInDir(itemPath)
                if (children && children.length) {
                    data.children = children
                }
                result.push(data);
            } else {
                // 文件
                result.push({
                    type: 'file',
                    name: item
                });
            }
        });
        return result;
    },
    /**
     * where 基本构建
     * @param obj 参数对象
     * @param where where
     * @param field 查询字段
     * @returns {{}} where
     */
    whereBase(obj,where, field = []) {
        let res = where?{}:where;
        for (let f in field) {
            if (obj[f] || obj[f] == 0) {
                res[f] = obj[f];
            }
        }
        return res;
    },
    /**
     * where 非空构建
     * @param obj 参数
     * @param where where对象
     * @param field 字段
     * @param wobj 筛选器对象
     */
    whereNoNull(obj, where, field, wobj) {
        if (obj[field] || obj[field] == 0) {
            where[field] = wobj;
        }
    }
};
module.exports = tool;